﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace CommonNet48.Encrypt
{
    /// <summary>
    /// DES加密解密帮助类
    /// </summary>
    public class DESHelper
    {

        /// <summary>
        /// 获取DES加密有效密码(要求密码长度为8的倍数)。
        /// </summary>
        /// <param name="keySour">传入的原始密码</param>
        /// <returns></returns>
        private static string GetValidKey(string keySour)
        {
            decimal baseLen = 8;
            if (keySour.Length < baseLen)
            {
                return string.Empty;
            }
            else
            {
                int len = Convert.ToInt32(baseLen * Math.Floor(keySour.Length / baseLen));
                return keySour.Substring(0, len);
            }
        }

        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="str">明文(待加密)</param>
        /// <param name="key1">加密Key1(长度必需是8的倍数)</param>
        /// <param name="key2">加密Key2(长度必需是8的倍数)</param>
        /// <returns>加密后的密文</returns>
        public static string Encrypt(string str, string key1 = "88888888", string key2 = "6666666666666666")
        {
            string strKey = GetValidKey(key1);
            string strIV = GetValidKey(key2);
            if (strKey.Length == 0 || strIV.Length == 0)
            {
                throw new Exception("加密Key不符合规范，无法加密！");
            }
            byte[] rgbKey = Encoding.UTF8.GetBytes(strKey);
            byte[] rgbIV = Encoding.UTF8.GetBytes(strIV);
            byte[] inputByteArray = Encoding.UTF8.GetBytes(str);
            DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }


        /// <summary>
        /// DES 解密
        /// </summary>
        /// <param name="str">密文(待解密)</param>
        /// <param name="key1">加密Key1(长度必需是8的倍数)</param>
        /// <param name="key2">加密Key2(长度必需是8的倍数)</param>
        /// <returns>解密后的明文</returns>
        public static string Decrypt(string str, string key1 = "88888888", string key2 = "6666666666666666")
        {
            string strKey = GetValidKey(key1);
            string strIV = GetValidKey(key2);
            if (strKey.Length == 0 || strIV.Length == 0)
            {
                throw new Exception("解密Key不符合规范，无法加密！");
            }
            byte[] rgbKey = Encoding.UTF8.GetBytes(strKey);
            byte[] rgbIV = Encoding.UTF8.GetBytes(strIV);
            byte[] inputByteArray = Convert.FromBase64String(str);
            DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return Encoding.UTF8.GetString(mStream.ToArray());
        }
    }
}
